package com.example.tools;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import com.example.pojos.Product;
import com.example.pojos.Product_Type;

public class XMLTools {
	
	public HashMap<Product_Type, ArrayList<Product>> getFeeds(){
		SAXParserFactory fact = SAXParserFactory.newInstance();
		SAXParser parser = null;
		HashMap<Product_Type, ArrayList<Product>> entries = null;
		
		try {
			parser = fact.newSAXParser();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		
		URL url = null;
		try {
			url = new URL("http://pagesperso-systeme.lip6.fr/Etienne.Renault/mylittlemarket/data.xml");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		ParserXMLHandler handler = new ParserXMLHandler();
		
		InputStream input;
		try {
			input = url.openStream();
			if(input==null) 
				return null;
			parser.parse(input, handler);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		
		entries = ((ParserXMLHandler)handler).getEntries();
		
		return entries;
	}
}
